﻿/*
    This file is part of nDoctor Light

    NDoctor is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    NDoctor is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with NDoctor.  If not, see <http://www.gnu.org/licenses/>.
*/
namespace Probel.Ldoc.ViewModels.Dto
{
    using System;
    using System.Diagnostics;

    using Probel.Ldoc.Services.Dto;
    using Probel.Ldoc.Services.Services;
    using Probel.Mvvm.Gui;

    [DebuggerDisplay("RecordCardViewModel [Id: {Id}]")]
    public class RecordCardViewModel : TreeViewModel
    {
        #region Constructors

        public RecordCardViewModel(RecordCardDto recordCard, IErrorService errorService, IPatientService service, bool isAsync)
            : base(errorService, service, isAsync)
        {
            this.Id = recordCard.Id;
            this.Name = recordCard.Name;

            this.Created = (recordCard.CreationDate.Date != DateTime.Today)
                ? recordCard.CreationDate.ToShortDateString()
                : recordCard.CreationDate.ToString("t");

            this.Updated = (recordCard.LastUpdate.Date != DateTime.Today)
                ? recordCard.LastUpdate.ToShortDateString()
                : recordCard.LastUpdate.ToString("t");
        }

        #endregion Constructors

        #region Properties

        public string Created
        {
            get;
            private set;
        }

        public string Updated
        {
            get;
            private set;
        }

        #endregion Properties

        #region Methods

        public void RefreshDates()
        {
            var recordCard = new RecordCardDto() { Id = this.Id };
            this.Service.RefreshCard(recordCard);

            this.Updated = (recordCard.LastUpdate.Date != DateTime.Today)
                ? recordCard.LastUpdate.ToShortDateString()
                : recordCard.LastUpdate.ToString("t");

            this.OnPropertyChanged(() => this.Updated);
        }

        public void RefreshTitle()
        {
            this.Name = this.Service.RefreshCard(new RecordCardDto() { Id = this.Id }).Name;
        }

        protected override void Rename()
        {
            if (this.Name == this.CachedName) { return; }

            this.Service.UpdateRecordName(new RecordCardDto() { Id = Id, Name = this.Name });
        }

        #endregion Methods
    }

    public static class RecordCardViewModelExtension
    {
        #region Methods

        public static RecordCardDto ToDto(this RecordCardViewModel @this)
        {
            return new RecordCardDto()
            {
                Id = @this.Id,
                Name = @this.Name,
            };
        }

        #endregion Methods
    }
}